using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace Tasks
{
	public class SerialTasks : Tasks
	{
		//[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action onComplete;

		public override IEnumerator GetEnumerator()
		{
			base.isRunning = true;
			UnityEngine.Debug.Log("Serialized Tasks Started, number of tasks: " + base.registeredEnumerators.Count);
			while (base.registeredEnumerators.Count > 0)
			{
				Stack<IEnumerator> stack = new Stack<IEnumerator>();
				IEnumerator task = base.registeredEnumerators[0];
				stack.Push(task);
				while (stack.Count > 0)
				{
					IEnumerator ce = stack.Peek();
					if (!ce.MoveNext())
					{
						stack.Pop();
					}
					else if (ce.Current != ce && ce.Current != null)
					{
						if (ce.Current is IEnumerable)
						{
							stack.Push(((IEnumerable)ce.Current).GetEnumerator());
						}
						else if (ce.Current is IEnumerator)
						{
							stack.Push(ce.Current as IEnumerator);
						}
						else if (ce.Current is WWW)
						{
							stack.Push(new SerialTasks.WWWEnumerator(ce.Current as WWW));
						}
					}
					if (ce is ParallelTasks)
					{
						UnityEngine.Debug.Log("New Set of Parallel Tasks Started from SerialTasks");
					}
					yield return null;
				}
				base.registeredEnumerators.RemoveAt(0);
			}
			base.isRunning = false;
			UnityEngine.Debug.Log("All Serialized Tasks Ended");
			if (this.onComplete != null)
			{
				this.onComplete();
			}
			yield break;
		}

		private class WWWEnumerator : IEnumerator
		{
			public WWWEnumerator(WWW www)
			{
				this._www = www;
			}

			public object Current
			{
				get
				{
					return this._www;
				}
			}

			public bool MoveNext()
			{
				return !this._www.isDone;
			}

			public void Reset()
			{
			}

			private WWW _www;
		}
	}
}
